// Copyright (c) 2009 Luca Marrocco.
// Modified or written by Luca Marrocco for inclusion with codej.
// Licensed under the Apache License, Version 2.0 (the "License")
package codej.ast;

import codej.ast.*;

public class Documentation extends Statement {

	private String documentation;

	public Documentation() {
	}

	public Documentation(String documentation) {
		setDocumentation(trim(documentation));
	}

	public void accept(Visitor visitor) {
		visitor.visit((Documentation) this);
	}

	public void setDocumentation(String documentation) {
		this.documentation = documentation;
	}

	private String trim(String documentation) {
		if (documentation.length() < 3)
			return documentation.trim();
		String substring = documentation.substring(3);
		if (documentation.matches("^//.*"))
			return substring.trim();
		if (documentation.matches("^//.*\n"))
			return substring.replaceAll("\n", "").trim();
		if (documentation.matches("^//.*\r"))
			return substring.replaceAll("\r", "").trim();
		if (documentation.matches("^/*.*"))
			return substring.substring(0, documentation.length() - 5).trim();
		if (documentation.matches("^/*.*\n"))
			return substring.substring(0, documentation.length() - 5).trim();
		if (documentation.matches("^/*.*\r"))
			return substring.substring(0, documentation.length() - 5).trim();
		return documentation;
	}
}
